package com.hdu.jdk;

import com.hdu.IStudentService;
import com.hdu.StudentService;
import sun.misc.ProxyGenerator;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Proxy;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

        StudentInvocationHandler studentInvocationHandler = new StudentInvocationHandler(new StudentService());

        IStudentService studentService = (IStudentService) Proxy.newProxyInstance(
                StudentService.class.getClassLoader(),
                new Class[]{IStudentService.class},
                studentInvocationHandler
        );

        studentService.save("张三");
        //before save
        //save student
        //after save
    }

    private static void getProxySourceCode() throws FileNotFoundException {
        byte[] $Proxy0s = ProxyGenerator.generateProxyClass("$Proxy0", StudentService.class.getInterfaces());
        FileOutputStream fileOutputStream = new FileOutputStream(
                "D:\\$Proxy0.class"
        );
        try {
            fileOutputStream.write($Proxy0s);
        } catch (IOException e) {

        }
    }
}
